var shareApi = 'https://api.igoyp.com/'
var selfApi = "http:/127.0.0.1/api.igoyp.com/public/"
var app = getApp()

// 获取服务器数据
function getShareApi(urlTail, param, callback) {
   var aipUrl = shareApi + urlTail
   request(aipUrl, param, callback)
}
// 获取本地数据
function getSelfApi(urlTail, param, callback) {
   var aipUrl = selfApi + urlTail
   request(aipUrl, param, callback)
}
// 请求数据
function request(aipUrl, param, callback) {
   wx.request({
      url: aipUrl,
      data: param,
      method: 'POST',
      success: function (res) {
         console.log('【地址】 ' + aipUrl, '\n【参数】', param, '\n【成功】', res);
         var d = res.data
         // callback(d)
         // if (d == '') return
         if (typeof (callback) != 'function') return
         if (d.code > 29300 && d.code < 30000) {
            toast(d.msg)
         } else if (typeof (callback) == 'function') {
            callback(d)
         }
      },
      fail: function (res) {
         toast('网络请求失败！')
         console.log('【地址】 ' + aipUrl, '\n【参数】', param, '\n【失败】', res);
      },
   })
}
// 获取session
function getWxSession(param, callback) {
   wx.login({
      success: function (res) {
         param.code = res.code
         getShareApi('weixin/Api/getSessionKeyOUid', param, function (res) {
            if (typeof (callback) == 'function') {
               callback(res)
            }
         })
      },
      fail: function (res) { },
   })
}
// 带图标弹窗提醒
function toast(msg, icon) {
   wx.showToast({
      title: msg,
      icon: icon ? icon : 'succes',
      image: '/resource/images/igoyp_logo_144x.png',
      duration: 1000,
      mask: true
   })
}
//提示
function modal(msg, title) {
   wx.showModal({
      title: title ? title : '温馨提示',
      content: msg,
   })
}
//增加key和value
function addValueForKey(toDict, fromDict, keyArray = []) {
   if (!isArray(keyArray)) {
      toast('第3个传参类型错误')
      return toDict
   }
   var len = keyArray.length
   if (len == 0) {
      for (var key in fromDict) {
         toDict[key] = fromDict[key]
      }
   } else {
      for (var i = 0; i < len; i++) {
         key = keyArray[i]

         if (fromDict[key]) {
            console.log('有key[' + i + ']==' + key)
            toDict[key] = fromDict[key]
         } else {
            console.log('没key[' + i + ']==' + key)
         }
      }
   }
   return toDict
}
//是否数组
function isArray(obj) {
   var res = Object.prototype.toString.call(obj)
   console.log('【检查是否数组Object.prototype.toString.call】', res)
   return res == '[object Array]'
}
//从对象获取经纬度
function getLoc(obj) {
   return {
      latitude: obj.latitude,
      longitude: obj.longitude
   }
}
function getNowFormatDate(type) {
   //type==1,只要日期 如果type==2 要日期时间
   var date = new Date();
   var seperator1 = "-";
   var seperator2 = ":";
   var month = date.getMonth() + 1;
   var strDate = date.getDate();
   if (month >= 1 && month <= 9) {
      month = "0" + month;
   }
   if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
   }

   var currentDate = date.getFullYear() + seperator1 + month + seperator1 + strDate

   if (type == 2) {
      currentDate += " " + date.getHours() + seperator2 + date.getMinutes()
         + seperator2 + date.getSeconds();
   }

   return currentDate;
}

function getNowTime() {
   var date = new Date();
   var _symbol = ":"
   var nowTime = formatNumber(date.getHours()) + _symbol + formatNumber(date.getMinutes()) + _symbol + formatNumber(date.getSeconds())
   return nowTime;
}

function isEmpty(val) {
   if (val == undefined || val == null || val == '' || val == 'undefined') {
      return true;
   } else {
      return false;
   }
}

function formatTime(date) {
   var year = date.getFullYear()
   var month = date.getMonth() + 1
   var day = date.getDate()

   var hour = date.getHours()
   var minute = date.getMinutes()
   var second = date.getSeconds()

   return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
   n = n.toString()
   return n[1] ? n : '0' + n
}

module.exports = {
   formatTime: formatTime,
   isEmpty: isEmpty,
   getNowTime: getNowTime,
   getShareApi: getShareApi,
   getSelfApi: getSelfApi,
   toast: toast,
   modal: modal,
   isArray: isArray,
   addValueForKey: addValueForKey,
   getLoc: getLoc,
   shareApi: shareApi,
   selfApi: selfApi,
   getWxSession: getWxSession,
   getNowFormatDate: getNowFormatDate
}
